package com.common.core.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.text.TextUtils;

/**
 * Created by hai8108 on 16/7/14.
 */
public class AppUtils {
    /**
     * pcode	渠道号	 	字符串	是
     version	版本号	 	字符串	是
     devid
     */
    public final static String DEVICEID = "devid";
    public final static String APPVERSION = "version";
    public final static String APPCODE = "pcode";

    /**
     * 获取当前版本号
     * @return
     */
    public static String getVersion(Context context) {
        String versionName = "";
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pInfo = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }

    /**
     * 获取设备名称
     *
     * @return
     */
    public static String getDeviceName() {
        return Build.MODEL;
    }


    private static final String UMENG_CHANNEL = "UMENG_CHANNEL";
    /**
     * 获取当前渠道值
     * @return
     */
    public static String getChannelId(Context context) {
        String productCode = "0101" + getMetaDataValue(context, UMENG_CHANNEL);
        return productCode;
    }
    /**
     * 获取AndroidManifest中meta value值
     *
     * @param metaKey
     * @return
     */

    public static String getMetaDataValue(Context context, String metaKey) {

        String metaValue = "000000";
        try {
            PackageManager pm = context.getPackageManager();
            ApplicationInfo applicationInfo = pm.getApplicationInfo(context.getPackageName(),
                    PackageManager.GET_META_DATA);
            Object value = applicationInfo.metaData.get(metaKey);
            if (value != null) {
                metaValue = value.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return metaValue;
    }
    /**
     * 获取当前网络类型
     * @return
     */
    public static String getStringNetType() {
        String netType = "";
        ConnectivityManager connectMgr = (ConnectivityManager) ContextProvider.getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectMgr.getActiveNetworkInfo();
        if (info != null) {
            switch (info.getType()) {
                case ConnectivityManager.TYPE_WIFI:
                    netType = "wifi";

                    break;
                case ConnectivityManager.TYPE_MOBILE:
                    netType = "4G";
                    break;
            }
        }
        return netType;
    }
    private static String getMacAddress(Context context) {
        String macAddress = null;
        WifiInfo wifiInfo = ((WifiManager) context.getSystemService(Context.WIFI_SERVICE))
                .getConnectionInfo();
        if (wifiInfo != null) {
            macAddress = wifiInfo.getMacAddress();
            if (macAddress == null || macAddress.length() <= 0) {
                return "";
            } else {
                return macAddress;
            }
        } else {
            return "";
        }
    }

    public static String generateDeviceId(Context context) {
        String deviceId = (String) SpTools.getValue(context, DEVICEID, "");
        if (TextUtils.isEmpty(deviceId)) {
            deviceId = StoreUtils.getRelevantData(context, "device_info");
            if (TextUtils.isEmpty(deviceId)) {
                String synthesize = CommonCoreUtil.getIMEI() + getMacAddress(context);
                deviceId =  MD5Tool.toMd5(synthesize);

                SpTools.saveString(context, DEVICEID, deviceId);
                StoreUtils.saveRelevantData(context, "device_info", deviceId);
            } else {
                SpTools.saveString(context, DEVICEID, deviceId);
            }
        }
        return deviceId;
    }
}
